//
//  ZCredpacketsVC.m
//  zctx
//
//  Created by apple on 16/1/22.
//  Copyright © 2016年 sanliang. All rights reserved.
//

#import "ZCredpacketsVC.h"
#import "ZCRedCell.h"
#import "ZCRedExplainVC.h"
#import "ZCredpackOldVC.h"
#import "ZCRedpackModelContent.h"
@interface ZCredpacketsVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UILabel *numLable;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSArray *dataArray;

@end

@implementation ZCredpacketsVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCredpacketsVC"];
    [MTA trackPageViewBegin:@"ZCredpacketsVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCredpacketsVC"];
    [MTA trackPageViewEnd:@"ZCredpacketsVC"];
    if (self.ispay !=1) {
        self.tabBarController.hidesBottomBarWhenPushed = NO;
    }
}

-(void)updateRequest{
    if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus >0) {
        self.bannerView.hidden = YES;
    }
    [self requestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //AF 开启监听网络状态
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
    self.navigationItem.title = @"我的红包";
    [self initnavegation];
    self.view.backgroundColor =[UIColor colorWithHexString:@"#ebeded"];
    self.dataArray = [[NSArray alloc]init];
//    NSLog(@"ssss111 = %ld",self.dataArray.count);

    [self requestData]; 
    
    [self initTableView];
    
    [self initheadView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCRedCell" bundle:nil] forCellReuseIdentifier:@"redsCell"];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 我的数据
-(void)requestData{
    NSString *rpid = nil;
    if (self.rp_id) {
        rpid = self.rp_id;
    }else{
        rpid = @"0";
    }
    [NetworkManager requestGetMyRedpackWithmid:UserModel.mid password:UserModel.password type:@"0" pagecount:@"0" rp_id:rpid success:^(NSDictionary *dictResponse) {
        if ([[dictResponse objectForKey:@"content"] isKindOfClass:[NSArray class]]){
                NSArray *sarray = (NSArray *)[dictResponse objectForKey:@"content"];
                if (sarray.count>0) {
                    self.dataArray = [sarray copy];
                    [self.tableView reloadData];
                }
            NSLog(@"ssss = %ld",self.dataArray.count);
            NSInteger colorRange = 0;
            NSInteger total = self.dataArray.count;
            if (total>=0 && total<10) {
                colorRange = 1;
            }else if (total>=10 && total<100){
                colorRange = 2;
            }else if (total>=100 && total<1000){
                colorRange = 3;
            }
            if (total == 0) {
                self.numLable.text = @"";
                self.numLable.hidden = YES;
            }else{
                NSString *numstr = [NSString stringWithFormat:@"有%ld个红包可用",total];
                NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:numstr];
                [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#f8a00a"] range:NSMakeRange(1, colorRange)];
                self.numLable.attributedText = str;
                self.numLable.hidden = NO;
            }
            }
    } failure:^(NSError *error) {
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == 0) {
            self.bannerView.hidden = NO;
        }
    }];
}

-(void)initheadView{
    
    UIView *headView = [[UIView alloc]init];
    [self.view addSubview:headView];
    headView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    [headView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).with.offset(0);
        make.top.equalTo (self.view.top).with.offset(64);
        make.height.mas_equalTo(43);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    self.numLable = [[UILabel alloc]init];
    [self.view addSubview:self.numLable];
    [self.numLable makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView.left).with.offset(18);
        make.centerY.equalTo (headView.centerY).with.offset(0);
        make.height.mas_equalTo(21);
        make.width.mas_equalTo(150);
    }];
    self.numLable.textColor = [UIColor colorWithHexString:@"#8a8a8a"];
    self.numLable.font = [UIFont systemFontOfSize:13];
    
    UIButton *redButton = [[UIButton alloc]init];
    [self.view addSubview:redButton];
    [redButton makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(headView.right).with.offset(-18);
        make.centerY.equalTo (headView.centerY).with.offset(0);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(60);
    }];
    redButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    redButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [redButton setTitle:@"红包说明" forState:UIControlStateNormal];
    [redButton setTitleColor:[UIColor colorWithHexString:@"#8a8a8a"] forState:UIControlStateNormal];
    [redButton addTarget:self action:@selector(ClickRedButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *rightImg = [[UIImageView alloc]init];
    [self.view addSubview:rightImg];
    [rightImg makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(redButton.left).with.offset(0);
        make.centerY.equalTo (headView.centerY).with.offset(0);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(15);
    }];
    [rightImg setImage:[UIImage imageNamed:@"me_red_wen"]];
}
-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(44, 0, 0, 0));
    }];
}
#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"redsCell";
    ZCRedCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [(ZCRedCell *)[ZCRedCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    if (self.ispay==1) {
        cell.isshow = YES;
    }
    ZCRedpackModelContent *dict =[ZCRedpackModelContent modelObjectWithDictionary:self.dataArray[indexPath.row]];
    cell.redpackModel = dict;
    NSIndexPath *firstPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView selectRowAtIndexPath:firstPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.ispay ==1) {
        ZCRedpackModelContent *dict =[ZCRedpackModelContent modelObjectWithDictionary:self.dataArray[indexPath.row]];
        self.returnRedblock(dict.cash,dict.rpId);
        [self.navigationController popViewControllerAnimated:YES];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 130;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (self.ispay ==1) {
        if (self.dataArray.count>0) {
            UIView *footView = [[UIView alloc]init];
            return footView;
        }else{
            UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,400)];
            footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
            
            UIImageView*redImg = [[UIImageView alloc]init];
            [footView addSubview:redImg];
            [redImg makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(footView.top).with.offset(0.255 * SCREEN_HEIGHT-44);
                make.centerX.equalTo (footView.centerX).with.offset(0);
                make.height.mas_equalTo(100);
                make.width.mas_equalTo(100);
            }];
            [redImg setImage:[UIImage imageNamed:@"me_red_money"]];
            
            
            UILabel*redLable = [[UILabel alloc]init];
            [footView addSubview:redLable];
            [redLable makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(redImg.bottom).with.offset(23);
                make.centerX.equalTo (footView.centerX).with.offset(0);
                make.height.mas_equalTo(21);
                make.width.mas_equalTo(150);
            }];
            redLable.textAlignment = NSTextAlignmentCenter;
            redLable.text = @"无可用红包";
            redLable.textColor = [UIColor colorWithHexString:@"#d2d2d2"];
            redLable.font = [UIFont systemFontOfSize:16];
            return footView;
        }
    }else{
        if (self.dataArray.count>0) {
            UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH, 60)];
            footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
            
            UIButton *lookoldButton = [[UIButton alloc]init];
            [footView addSubview:lookoldButton];
            [lookoldButton makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(footView.centerX).with.offset(0);
                make.centerY.equalTo (footView.centerY).with.offset(0);
                make.height.mas_equalTo(44);
                make.width.mas_equalTo(SCREEN_WIDTH/2);
            }];
            lookoldButton.titleLabel.font = [UIFont systemFontOfSize:14];
            [lookoldButton setTitle:@"查看历史红包" forState:UIControlStateNormal];
            [lookoldButton setTitleColor:[UIColor colorWithHexString:@"#8a8a8a"] forState:UIControlStateNormal];
            [lookoldButton addTarget:self action:@selector(lookOldredpack) forControlEvents:UIControlEventTouchUpInside];
            
            return footView;
        }else{
            
            UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,SCREEN_WIDTH,400)];
            footView.backgroundColor = [UIColor colorWithHexString:@"#fafcfb"];
            
            UIImageView*redImg = [[UIImageView alloc]init];
            [footView addSubview:redImg];
            [redImg makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(footView.top).with.offset(0.255 * SCREEN_HEIGHT-44);
                make.centerX.equalTo (footView.centerX).with.offset(0);
                make.height.mas_equalTo(100);
                make.width.mas_equalTo(100);
            }];
            [redImg setImage:[UIImage imageNamed:@"me_red_money"]];
            
            
            UILabel*redLable = [[UILabel alloc]init];
            [footView addSubview:redLable];
            [redLable makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(redImg.bottom).with.offset(24);
                make.centerX.equalTo (footView.centerX).with.offset(0);
                make.height.mas_equalTo(21);
                make.width.mas_equalTo(150);
            }];
            redLable.textAlignment = NSTextAlignmentCenter;
            redLable.text = @"无可用红包";
            redLable.textColor = [UIColor colorWithHexString:@"#d2d2d2"];
            redLable.font = [UIFont systemFontOfSize:16];
            
            UIButton *lookoldButton = [[UIButton alloc]init];
            [footView addSubview:lookoldButton];
            [lookoldButton makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(redLable.bottom).with.offset(10);
                make.centerX.equalTo (footView.centerX).with.offset(0);
                make.height.mas_equalTo(21);
                make.width.mas_equalTo(SCREEN_WIDTH/2);
            }];
            lookoldButton.titleLabel.numberOfLines = 0;
            lookoldButton.titleLabel.font = [UIFont systemFontOfSize:14];
            [lookoldButton setTitle:@"查看历史红包" forState:UIControlStateNormal];
            [lookoldButton setTitleColor:[UIColor colorWithHexString:@"#d2d2d2"] forState:UIControlStateNormal];
            [lookoldButton addTarget:self action:@selector(lookOldredpack) forControlEvents:UIControlEventTouchUpInside];
            return footView;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (self.ispay ==1) {
        if (self.dataArray.count>0) {
            return 0.0001;
        }else{
            return 400;
        }
    }else{
        if (self.dataArray.count>0) {
            return 100;
        }else{
            return 400;
        }
    }
}

#pragma mark 红包说明
-(void)ClickRedButton{
    ZCRedExplainVC *explainvc = [[ZCRedExplainVC alloc]init];
    [self.navigationController pushViewController:explainvc animated:YES];
}

-(void)lookOldredpack{
    ZCredpackOldVC *zcred = [[ZCredpackOldVC alloc]init];
    [self.navigationController pushViewController:zcred animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
